package cn.acewill.yxd.yunxundian.ui.follow_up.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.ui.follow_up.FollowUpRowActivity;
import cn.acewill.yxd.yunxundian.ui.follow_up.nodeStruct.FU_main;

/**
 * Created by user_001 on 2017/12/15.
 */

public class FollowUpMainAdapter extends RecyclerView.Adapter<FollowUpMainAdapter.MyViewHolder> implements Filterable {
    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private List<FU_main> mDataList;
    private FollowUpMainFilter mFilter;

    public FollowUpMainAdapter(Context context, List<FU_main> dataList) {
        mDataList = dataList;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(mLayoutInflater.inflate(R.layout.card_follow_up_main, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final int adapterPosition = position;
        holder.schName.setText(mDataList.get(position).schName);
        holder.shopName.setText(mDataList.get(position).shopName);
        holder.empName.setText(mDataList.get(position).empName);
        holder.exTime.setText(mDataList.get(position).exTime);
        holder.fuCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                //Log.i("FollowUpMainAdapter", "onClick--> position = " + adapterPosition);
                Intent intent = new Intent(mContext, FollowUpRowActivity.class);
                intent.putExtra("node", mDataList.get(adapterPosition));
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{
        @ViewInject(R.id.fuCard)
        private CardView fuCard;
        @ViewInject(R.id.schName)
        private TextView schName;
        @ViewInject(R.id.shopName)
        private TextView shopName;
        @ViewInject(R.id.empName)
        private TextView empName;
        @ViewInject(R.id.exTime)
        private TextView exTime;

        MyViewHolder(View view){
            super(view);
            x.view().inject(this, view);
        }
    }

    @Override
    public Filter getFilter() {
        if(mFilter == null)
            mFilter = new FollowUpMainFilter(this, mDataList);
        return mFilter;
    }

    private static class FollowUpMainFilter extends Filter {

        private final FollowUpMainAdapter adapter;

        private final List<FU_main> originalList;

        private final List<FU_main> filteredList;

        private FollowUpMainFilter(FollowUpMainAdapter adapter, List<FU_main> originalList) {
            super();
            this.adapter = adapter;
            this.originalList = new LinkedList<>(originalList);
            this.filteredList = new ArrayList<>();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            filteredList.clear();
            final FilterResults results = new FilterResults();

            if (constraint.length() == 0) {
                filteredList.addAll(originalList);
            } else {
                final String filterPattern = constraint.toString().toLowerCase().trim();

                for (final FU_main fumNode : originalList) {
                    if (fumNode.schName.contains(filterPattern) || fumNode.exTime.contains(filterPattern)) {
                        filteredList.add(fumNode);
                    }
                }
            }
            results.values = filteredList;
            results.count = filteredList.size();
            return results;
        }

        @Override
        @SuppressWarnings("unchecked")
        protected void publishResults(CharSequence constraint, FilterResults results) {
            adapter.mDataList.clear();
            adapter.mDataList.addAll((ArrayList<FU_main>) results.values);
            adapter.notifyDataSetChanged();
        }
    }
}
